#include <iostream>

using std::cout;
using std::endl;

//1、引用是可以作为函数参数
//值传递--->副本
//拷贝的时候会更占用内存
void swap1(int x, int y)//int x = a, int y = b副本
{
    cout << "x = " << x << ", y = " << y << endl;
    int tmp = x;
    x = y;
    y = tmp;
    cout << "x = " << x << ", y = " << y << endl;
}

//值传递---->地址值（地址传递）
//不够直观(占用指针大小的空间)
void swap2(int *px, int *py)//int *px = &a, int *py = &b
{
    cout << "*px = " << *px << ", *py = " << *py << endl;
    int tmp = *px;
    *px = *py;
    *py = tmp;
    cout << "*px = " << *px << ", *py = " << *py << endl;
}

//引用传递---->非常直接，可读性性强
void swap3(int &x, int &y)//int &x = a, int &y = b
{
    cout << "x = " << x << ", y = " << y << endl;
    int tmp = x;
    x = y;
    y = tmp;
    cout << "x = " << x << ", y = " << y << endl;
}

void test()
{
    int a = 3;
    int b = 4;
    cout << "a = " << a << ", b = " << b << endl;
    swap3(a, b);
    cout << "a = " << a << ", b = " << b << endl;
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

